/**
 * Created by Nie on 2015/12/10.
 */
app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
    $routeProvider.
    when('/cooperate', {
        templateUrl: 'cooperate/list.html',
        controller: 'cooperateListController'
    }).
    when('/cooperateEdit/cooperate_id/:cooperate_id', {
        templateUrl: 'cooperate/edit.html',
        controller: 'cooperateEditController'
    }).
    when('/cooperate/detail/:cooperate_id', {
        templateUrl: 'cooperate/detail.html',
        controller: 'cooperateEditController'
    }).
    when('/cooperate/release_notes', {
        templateUrl: 'cooperate/release_notes.html',
        controller: 'notesListController'
    }).
    when('/cooperate/release_notes/detail/:cooperate_id', {
        templateUrl: 'cooperate/release_notes_detail.html',
        controller: 'notesDetailController'
    }).
    when('/cooperate/apply', {
        templateUrl: 'cooperate/apply.html',
        controller: 'applyListController1'
    });
}]);

app.factory('cooperateService', ['$http', '$location', cooperateService]);
app.controller('cooperateListController', ['$scope', '$rootScope', '$window', '$location', '$http', '$routeParams', 'cooperateService', 'servantService', 'orderService', cooperateListController]);
app.controller('cooperateEditController', ['$scope', '$rootScope', '$window', '$location', '$http', '$routeParams', 'orderService', cooperateEditController]);
app.controller('notesListController', ['$scope', '$rootScope', '$window', '$location', '$http', '$routeParams', 'cooperateService', 'servantService', 'orderService', notesListController]);
app.controller('notesDetailController', ['$scope', '$rootScope', '$window', '$location', '$http', '$routeParams', 'orderService', 'cooperateService', notesDetailController]);
app.controller('applyListController1', ['$scope', '$rootScope', '$window', '$location', '$http', '$routeParams', 'cooperateService', 'servantService', 'orderService', applyListController1]);

function cooperateService($http, $location) {
    return {
        applyList: function (data, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'order/apply/list',
                'data': data,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        },
        delete: function (cooperate_id, fn) {
            var data = {
                'cooperate_id': cooperate_id
            };
            baas_jz.call({
                'http': $http,
                'method': 'post',
                'data': data,
                'uri': 'cooperate/cooperateList/delete',
                'success': function (d) {

                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        },
        checkApply: function (apply_id, response, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'post',
                'uri': 'order/apply/review/' + apply_id + "/" + response,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
    };
}

function cooperateListController($scope, $rootScope, $window, $location, $http, $routeParams, cooperateService, servantService, orderService) {
    $scope.reason = "";
    $scope.search = {
        limit: 20,
        page: 1,
        public_show: 1,
        public: 1//为合作订单， 其他值 为订单
    };
    //获取服务类型列表
    $scope.getserviceType1 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList1 = d.list;
        });
    }
    $scope.getserviceType2 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList2 = d.list;
        });
    }
    $scope.getserviceType3 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList3 = d.list;
        });
    }

    $scope.getserviceType1('0');

    $scope.getList = function () {
        orderService.searchList($scope.search, function (json) {
            $scope.list = json['list'];
            $scope.count = json['count'];
            $scope.currentPage = 1;
            $scope.getPage(json['count']);
            $scope.setApplyBtn();
        });

    }
    $scope.getList();
    //搜索
    $scope.searchCooperate = function (data) {
        $scope.search.start_time = angular.element("#start_time").val();
        $scope.search.end_time = angular.element("#end_time").val();
        $scope.search.page = 1;
        orderService.searchList($scope.search, function (json) {
            $scope.list = json['list'];
            $scope.count = json['count'];
            $scope.currentPage = 1;
            $scope.getPage(json['count']);
            $scope.setApplyBtn();
        });
    }

    $scope.getCompany = function () {
        orderService.getCompany($scope.app_id, function (json) {
            $scope.companylist = json.list;
        });
    }
    $scope.getCompany();
    /*分页*/
    $scope.changePage = function (p) {
        $scope.search.page = p;
        orderService.searchList($scope.search, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
            $scope.setApplyBtn();
        });
    };
    $scope.getPage = function (count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
        }
    };
    $scope.setApplyBtn = function () {
        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        for (i = 0; i < $scope.list.length; i++) {
            var item = $scope.list[i];
            for (var j = 0; j < item.record_company_code.length; j++) {
                var com = item.record_company_code[j];
                if (com == manager.company_code) {
                    item.apply_status = true;
                }
            }

        }
    }


    $scope.cancel = function (cooperate_id) {
        var data = {
            'selected_company_code': '',
            'selected_company_name': '',
            'cooperate_id': cooperate_id
        };
        cooperateService.update(data, function (d) {
            if (d) {
                $scope.getList();
            } else {
                alert("更新失败");
            }
        });
    };
    $scope.updatecooperateStatus = function (cooperate_id, status) {
        var data = {
            'status': status,
            'reason': "",
            'cooperate_id': cooperate_id
        };
        cooperateService.update(data, function (d) {
            if (d) {
                $scope.getList();
            } else {
                alert("更新失败");
            }
        });
    };
    $scope.delete = function (cooperate_id) {
        cooperateService.delete(cooperate_id, function (json) {
            $scope.searchcooperate($scope.search);
            //$scope.getList();
        });
    }
    $scope.changeServant = function (cooperate_id, servant_id) {
        if (servant_id == null || servant_id == "") {
            alert("请选择服务人员");
        } else {
            var data = {
                'servant_id': servant_id,
                'cooperate_id': cooperate_id
            };
            baas_jz.call({
                'http': $http,
                'method': 'post',
                'uri': 'cooperate/cooperate/grab/' + cooperate_id + '/' + servant_id,
                'success': function (d) {
                    if (d["status"] != 0) {
                        alert("服务人员选择成功");
                    } else {
                        alert(d["description"]);
                    }
                }
            });
        }
    }
    $scope.openWindow = function (cooperate_id) {
        $scope.cooperate_id = cooperate_id;
    }
    $scope.cooperateAmount = function (money) {
        var data = {
            amount: money,
            cooperate_id: $scope.cooperate_id,
            status: "完成",
        }
        cooperateService.update(data, function (d) {
            if (d) {
                closeMod();
                $scope.getList();
            } else {
                alert("更新失败");
            }
        });
    }
    $scope.saveBackcooperate = function (reason, status) {
        if (reason.length == 0) {
            alert("请填写终止服务\退单原因");
        } else {
            var data = {
                'status': status,
                'cooperate_id': $scope.cooperate_id,
                'reason': reason
            };

            cooperateService.update(data, function (d) {
                if (d) {
                    closeMod();
                    $scope.getList();
                } else {
                    alert("更新失败");
                }
            });
        }
    }
    $scope.getSelCompany = function (cooperate_id) {
        $scope.cooperate_id = cooperate_id;
    }
    $scope.saveSelCompany = function (company) {
        if (company == undefined || company == "") {
            alert("请选择派单企业");
        } else {
            var data = {
                'selected_company_code': company.code,
                'selected_company_name': company.name,
                'status': '待受理',
                'cooperate_id': $scope.cooperate_id
            };
            cooperateService.update(data, function (d) {
                if (d) {
                    closeMod();
                    alert("派单成功")
                    $scope.getList();
                } else {
                    alert("更新失败");
                }
            });
        }

    }

    $scope.export = function (data) {
        var url = WEB_URL + '/cooperate/cooperateList/export';

        cooperateService.export(data, function (d) {
            if (d.status) {
                $scope.export_url = d.url;
                angular.element('#exportUrl').show();
            }
            else
                alert('网络错误，请重新导出');
        });

    };
    $scope.selServant = function (obj) {
        $scope.searchOfServant.jobs = obj.serviceType2_code;

    };
    ////////////////////////////从业人员筛选//////////////////////////////

    $scope.selectedPageOfServant = 1;
    $scope.limitOfServant = 5;
    $scope.searchOfServant = {
        //servantstatus:"待岗",
        verifystatus: "已通过审核"
    };

    $scope.getServantList = function () {
        $scope.selectedPageOfServant = 1;

        console.log($scope.searchOfServant);
        servantService.list($scope.searchOfServant, $scope.limitOfServant, $scope.selectedPageOfServant, $scope.app_id, function (json) {
            $scope.servantList = json['list'];
            console.log($scope.servantList);
            $scope.countOfServant = json['count'];
            $scope.currentPageOfServant = 1;
            $scope.getPageOfServant($scope.countOfServant);
        });

    };
    $scope.getPageOfServant = function (count) {
        $scope.pageListOfServant = [];
        var limit = $scope.limitOfServant;
        $scope.totalPageOfServant = parseInt(count / limit);
        $scope.totalPageOfServant += count % limit >= 1 ? 1 : 0;
        var start = ($scope.currentPageOfServant - 2) >= 1 ? $scope.currentPageOfServant - 2 : 1;
        var end = ($scope.currentPageOfServant + 2) <= $scope.totalPageOfServant ? $scope.currentPageOfServant + 2 : $scope.totalPageOfServant;
        for (var i = start; i <= end; i++) {
            $scope.pageListOfServant.push({
                number: i,
                active: i == $scope.currentPageOfServant ? 'active' : ''
            });
        }
    };
    $scope.changePageOfServant = function (page) {
        if (page == 0) {
            page = 1;
        }
        $scope.selectedPageOfServant = page;

        var manager = JSON.parse(localStorage.getItem(window.who + "manager"));
        if (manager != undefined && manager.isPension) {
            $scope.searchOfServant.manager_group_id = manager.manager_group_id;
        }
        servantService.list($scope.searchOfServant, $scope.limitOfServant, $scope.selectedPageOfServant, $scope.app_id, function (json) {
            $scope.servantList = json['list'];
            $scope.countOfServant = json['count'];
            $scope.currentPageOfServant = page;
            $scope.getPageOfServant($scope.countOfServant);
        });
    };

    $scope.selServantWin = function (order) {
        $scope.order = order;
        $scope.selServantStep = 1;
        $scope.searchOfServant = {
            //servantstatus: "待岗",
            verifystatus: "已通过审核"
        };
        $scope.searchOfServant.serviceType3 = {
            code: order.serviceType3_code
        }
        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        if (manager != undefined && manager.isPension) {
            $scope.searchOfServant.manager_group_id = manager.manager_group_id;

        }
        $scope.getServantList();
    };
    $scope.setServant = function (servant) {

        $scope.selServantStep = 2;
        $scope.servant = servant;
        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        $scope.apply_data = {
            company_contact_telephone: $scope.order.comPhone,
            company_contact: $scope.order.comName,
            company_code: manager.company_code,
            company_name: manager.company_name,
            name: $scope.order.name,
            phone: $scope.order.phone
        }

    };
    $scope.apply = function (apply_data) {
        if (apply_data.salary_rate == "" || apply_data.salary_rate == undefined) {
            alert("请输入分成比");
            return;
        }
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': apply_data,
            'uri': 'order/apply/request/order_id/' + $scope.order.order_id + '/servant_id/' + $scope.servant.servant_id,
            'success': function (d) {
                if (d["status"] != 0) {
                    alert("申请合作成功");
                } else {
                    alert(d["description"]);
                }
            }
        });
        closeMod();
    };

    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        init();
    });
}

function cooperateEditController($scope, $rootScope, $window, $location, $http, $routeParams, orderService) {
    $scope.order = {};

    //获取服务类型列表
    $scope.getserviceType1 = function (code) {
        orderService.getServiceListByCode(code, function (d) {
            $scope.serviceList1 = d.list;
            $scope.order.is_trial = "0";
            $scope.serviceList3 = [];
        });
    };
    $scope.getserviceType2 = function (code) {
        orderService.getServiceListByCode(code, function (d) {
            $scope.serviceList2 = d.list;
            $scope.order.is_trial = "0";
            $scope.serviceList3 = [];
        });
    };
    $scope.getserviceType3 = function (code) {
        orderService.getServiceListByCode(code, function (d) {
            $scope.serviceList3 = d.list;
            $scope.order.is_trial = "0";
        });
    };

    $scope.order_id = $routeParams.cooperate_id;
    if ($scope.order_id == 0) {
        $scope.getserviceType1('0');
    } else {
        $scope.getserviceType1('0');
        var data = {order_id: $scope.order_id};
        orderService.getOrderDetail(data, function (d) {
            $scope.order = d.orderinfo;
        });
    }

    $scope.getFilterCompany = function (serviceType3) {
        $scope.filterCompanyList = [];
        if (serviceType3 == "" || typeof serviceType3 == undefined || !serviceType3) {
            $scope.filterCompanyList = "";
            $scope.order.is_trial = 0;
        } else {
            //判断是否试工
            if (serviceType3["shigong"] == "是") {
                $scope.order.is_trial = "1";
            } else {
                $scope.order.is_trial = "0";
            }
            var code = serviceType3.code;
            angular.forEach($scope.companylist, function (data, index, list) {
                if (typeof data.serviceType3_code != "undefined" && data.serviceType3_code.indexOf(code) > -1) {
                    $scope.filterCompanyList.push(data);
                }
            });
        }
    };

    /*添加订单*/
    $scope.edit = function (data) {
        data.time = angular.element("#time").val();
        data.public = 1;
        data.serviceType1_code = (data != '' && data != undefined && data.serviceType1 != undefined) ? data.serviceType1.code : '';
        data.serviceType1_title = (data != '' && data != undefined && data.serviceType1 != undefined) ? data.serviceType1.title : '';
        data.serviceType2_code = (data != '' && data != undefined && data.serviceType2 != undefined) ? data.serviceType2.code : '';
        data.serviceType2_title = (data != '' && data != undefined && data.serviceType2 != undefined) ? data.serviceType2.title : '';
        data.serviceType3_code = (data != '' && data != undefined && data.serviceType3 != undefined) ? data.serviceType3.code : '';
        data.serviceType3_title = (data != '' && data != undefined && data.serviceType3 != undefined) ? data.serviceType3.title : '';
        data.serviceType1 = (data != '' && data != undefined && data.serviceType1 != undefined) ? data.serviceType1.code : '';
        data.serviceType2 = (data != '' && data != undefined && data.serviceType2 != undefined) ? data.serviceType2.code : '';
        data.serviceType3 = (data != '' && data != undefined && data.serviceType3 != undefined) ? data.serviceType3.code : '';
        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        data.record_company_code = manager.company_code;
        data.record_company_name = manager.company_name;

        if (data.serviceType3 == "" || typeof data.serviceType3 == undefined || !data.serviceType3) {
            alert("请选择服务项目，精确到服务小类");
            return;
        } else {
            var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
            if (manager.serviceType3_code == undefined || manager.serviceType3_code == "") {
                alert("您的公司没有选定服务项目范围，无法添加合作订单");
                return;
            }
            var isCheck = false;
            for (i = 0; i < manager.serviceType3_code.length; i++) {
                if (data.serviceType3_code == manager.serviceType3_code[i]) {
                    isCheck = true;
                }
            }
            if (!isCheck) {
                alert("您的公司没有提供 [" + data.serviceType3_title + "] 服务，无法添加该服务项目订单");
                return;
            }
        }

        if (data.name == "" || data.name == undefined) {
            alert("请输入联系人");
            return;
        }
        if (data.phone == "" || data.phone == undefined) {
            alert("请输入联系电话");
            return;
        }
        if (data.comName == "" || data.comName == undefined) {
            alert("请输入企业联系人");
            return;
        }
        if (data.comPhone == "" || data.comPhone == undefined) {
            alert("请输入企业联系电话");
            return;
        }
        orderService.addOrder(data, $scope.app_id, function (json) {
            alert("添加订单成功！");
            $location.path('/cooperate');
        });
    };
}

function notesDetailController($scope, $rootScope, $window, $location, $http, $routeParams, orderService, cooperateService) {
    //获取服务类型列表
    $scope.getserviceType1 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList1 = d.list;
        });
    }
    $scope.getserviceType2 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList2 = d.list;
        });
    }
    $scope.getserviceType3 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList3 = d.list;
        });
    }
    $scope.order_id = $routeParams.cooperate_id
    if ($scope.order_id == 0) {
        $scope.getserviceType1('0');
    } else {
        $scope.getserviceType1('0');
        var data = {order_id: $scope.order_id};
        orderService.detail(data, function (d) {
            $scope.order = d.orderinfo;
            $scope.applyList($scope.order);
        });
    }
    $scope.applyList = function (data) {
        $scope.searchApply = {
            limit: 20,
            page: 1,
            order_id: data.order_id
        }
        cooperateService.applyList($scope.searchApply, function (json) {
            $scope.list = json['list'];
            console.log($scope.list);
            $scope.count = json['count'];
            $scope.currentPage = 1;
            $scope.getPage(json['count']);
        });

    }
    /*分页*/
    $scope.changePage = function (p) {
        $scope.searchApply.page = p;
        orderService.applyList($scope.searchApply, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
        });
    };
    $scope.getPage = function (count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
        }
    };

    /*
     * 审核合作申请
     * @param {Object} obj
     * @param {Object} response
     */
    $scope.check_apply = function (obj, response) {
        console.log($scope.order);
        console.log(obj);
        cooperateService.checkApply(obj._id.$id, response, function (d) {
            console.log(d);
            if (response != "accept") {
                $scope.applyList($scope.order);
            }
        });
        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        var order_data = {
            public: 0,
            order_id: $scope.order_id,
            selected_company_code: manager.company_code,
            selected_company_name: manager.company_name,
            selected_servant_id: obj.servant_id,
            selected_servant_name: obj.servant_name,
            status: "已接单"


        }
        if (response == "accept") {
            for (i = 0; i < $scope.list.length; i++) {
                var item = $scope.list[i];
                if (item._id.$id != obj._id.$id) {
                    cooperateService.checkApply(item._id.$id, "refuse", function (d) {
                        console.log(d);
                        if (response != "accept") {
                            $scope.applyList($scope.order);
                        }
                    });
                }
            }
            orderService.update(order_data, function (d) {
                console.log(d);
                $location.path('/cooperate/release_notes');
            });


        }
    }


}

function notesListController($scope, $rootScope, $window, $location, $http, $routeParams, cooperateService, servantService, orderService) {
    $scope.reason = "";
    var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
    $scope.search = {
        limit: 20,
        page: 1,
        record_company_code: manager.company_code,
        public: 1
    };
    //获取服务类型列表
    $scope.getserviceType1 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList1 = d.list;
        });
    }
    $scope.getserviceType2 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList2 = d.list;
        });
    }
    $scope.getserviceType3 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList3 = d.list;
        });
    }

    $scope.getserviceType1('0');
    $scope.getList = function () {
        console.log($scope.search);
        orderService.searchList($scope.search, function (json) {
            $scope.list = json['list'];
            console.log($scope.list);
            $scope.count = json['count'];
            $scope.currentPage = 1;
            $scope.getPage(json['count']);
        });

    }
    $scope.getList();
    //搜索
    $scope.searchCooperate = function (data) {
        $scope.search.start_time = angular.element("#start_time").val();
        $scope.search.end_time = angular.element("#end_time").val();
        $scope.search.page = 1;
        orderService.searchList($scope.search, function (json) {
            $scope.list = json['list'];
            $scope.count = json['count'];
            $scope.currentPage = 1;
            $scope.getPage(json['count']);
        });
    }

    $scope.getCompany = function () {
        orderService.getCompany($scope.app_id, function (json) {
            $scope.companylist = json.list;
        });
    }
    $scope.getCompany();
    /*分页*/
    $scope.changePage = function (p) {
        $scope.search.page = p;
        orderService.searchList($scope.search, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
        });
    };
    $scope.getPage = function (count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
        }
    };

    $scope.openWindow = function (cooperate_id) {
        $scope.cooperate_id = cooperate_id;
    }
    $scope.updateIsShow = function (order, show) {
        var data = {
            order_id: order.order_id,
            public_show: show
        }
        console.log(data);
        orderService.update(data, function (d) {
            console.log(d);
            $scope.getList();
        });
    }

    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        init();
    });
}

function applyListController1($scope, $rootScope, $window, $location, $http, $routeParams, cooperateService, servantService, orderService) {
    $scope.reason = "";
    $scope.search = {
        limit: 20,
        page: 1
    };

    //获得本公司的company_code by gxl 20151026
    var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
    $scope.company_code = manager.company_code;
    if (typeof  manager.serviceType3_code != 'undefined' && manager.serviceType3_code != "") {
        $scope.search.servant_company_code = manager.company_code;
    }

    //获取服务类型列表
    $scope.getserviceType1 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList1 = d.list;
        });
    }
    $scope.getserviceType2 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList2 = d.list;
        });
    }
    $scope.getserviceType3 = function (code) {
        orderService.serviceList(code, function (d) {
            $scope.serviceList3 = d.list;
        });
    }

    $scope.getserviceType1('0');
    $scope.getList = function () {
        console.log($scope.search);
        cooperateService.applyList($scope.search, function (json) {
            console.log(json['list']);
            $scope.list = json['list'];
            console.log($scope.list);
            $scope.count = json['count'];
            $scope.currentPage = 1;
            $scope.getPage(json['count']);
        });

    }
    $scope.getList();
    //搜索
    $scope.searchCooperate = function (data) {
        $scope.search.start_time = angular.element("#start_time").val();
        $scope.search.end_time = angular.element("#end_time").val();
        $scope.search.page = 1;
        if ($scope.search.start_time == "") {
            delete $scope.search.start_time;
        }
        if ($scope.search.end_time == "") {
            delete $scope.search.end_time;
        }
        if ($scope.search.order_id == "" || typeof $scope.search.order_id == 'undefined') {
            delete $scope.search.order_id;
        }
//          if($scope.search.address==""|| typeof $scope.search.address=='undefined'){
//	      		delete $scope.search.address;
//	      	}
        if ($scope.search.record_company_code == "" || typeof $scope.search.record_company_code == 'undefined') {
            delete $scope.search.record_company_code;
        }
        if ($scope.search.order_serviceType1_code == "" || typeof $scope.search.order_serviceType1_code == 'undefined' || $scope.search.order_serviceType1_code == null) {
            delete $scope.search.order_serviceType1_code;
            delete $scope.search.order_serviceType2_code;
            delete $scope.search.order_serviceType3_code;
        }
        if ($scope.search.order_serviceType2_code == "" || typeof $scope.search.order_serviceType2_code == 'undefined' || $scope.search.order_serviceType1_code == null) {
            delete $scope.search.order_serviceType2_code;
            delete $scope.search.order_serviceType3_code;
        }
        if ($scope.search.order_serviceType3_code == "" || typeof $scope.search.order_serviceType3_code == 'undefined' || $scope.search.order_serviceType2_code == null) {
            delete $scope.search.order_serviceType3_code;
        }
        if ($scope.search.selected_servant_name == "" || typeof $scope.search.selected_servant_name == 'undefined') {
            delete $scope.search.selected_servant_name;
        }
        if ($scope.search.public_fund == "" || typeof $scope.search.public_fund == 'undefined') {
            delete $scope.search.public_fund;
        }
        if ($scope.search.status_text == "" || typeof $scope.search.status_text == 'undefined') {
            delete $scope.search.status_text;
        }
        cooperateService.applyList($scope.search, function (json) {
            $scope.list = json['list'];
            $scope.count = json['count'];
            $scope.currentPage = 1;
            $scope.getPage(json['count']);
        });
    }

    $scope.getCompany = function () {
        orderService.getCompany($scope.app_id, function (json) {
            $scope.companylist = json.list;
        });
    }
    $scope.getCompany();
    /*分页*/
    $scope.changePage = function (p) {
        $scope.search.page = p;
        cooperateService.applyList($scope.search, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
        });
    };
    $scope.getPage = function (count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
        }
    };

    $scope.openWindow = function (cooperate_id) {
        $scope.cooperate_id = cooperate_id;
    }
    $scope.cancel = function (order) {
        var data = {
            order_id: order.order_id,
            public_show: 0
        }
        orderService.update(data, function (d) {
            console.log(d);
            $scope.getList();
        });
    }

    //撤销申请 by gxl 20151023

    $scope.cancelApply = function (apply_id) {

        baas_jz.call({
            'http': $http,
            'method': 'post',
            'uri': 'order/apply/revert/request/' + apply_id,
            'success': function (d) {
                if (d.status == 1) {
                    alert('取消成功！');
                } else {
                    if (d.description) {
                        alert(d.description);
                    } else {
                        alert('网络原因，请求失败，请稍后重试');
                    }
                }
            }
        });
    }


    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        init();
    });
}